package ua.khuta.myskidka;


import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class FragmentNewsInfo extends Fragment implements OnClickListener {
	View v;
	TextView tvCompanyName, tvNewsDescription;
	ImageView ivNewsPicture;
	Database db;
	ImageButton btnNewsStartCompanyInfo;
	int companyId;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		v = inflater.inflate(R.layout.fragment_news_info, null);
		initialization();
		Bundle bundl = getArguments();

		int position = bundl.getInt("position");
		db = Database.getInstance(getActivity());
		Cursor c = db.getNewsFromDb();
		Log.d("myLogs", "News Info position " + position);
		c.moveToPosition(position);
		// There must be name of company instead name of news
		tvCompanyName.setText(c.getString(2));
		tvNewsDescription.setText(c.getString(7));
		companyId=c.getInt(6);
		//ivNewsPicture.setImageBitmap(downloadBitmap(c.getString(4)));
		return v;
	}

	public void initialization() {
		tvCompanyName = (TextView) v.findViewById(R.id.tv_news_company_name);
		tvNewsDescription = (TextView) v.findViewById(R.id.tv_news_description);
		ivNewsPicture = (ImageView) v.findViewById(R.id.iv_news_picture);
		btnNewsStartCompanyInfo = (ImageButton) v
				.findViewById(R.id.btn_news_start_company_info);
		btnNewsStartCompanyInfo.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_news_start_company_info:
			Bundle pos = new Bundle();
			//
			//
			pos.putInt("companyId", companyId );
			//
			Fragment fragment = new FragmentPlaceInfo();
			fragment.setArguments(pos);
			FragmentTransaction transaction = getFragmentManager()
					.beginTransaction();
			transaction.replace(R.id.content_frame, fragment);
			transaction.addToBackStack(null);
			transaction.commit();
			break;

		default:
			break;
		}
		
	}
	

}
